﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Models;
using ElingOA6_Admin_DSJCJ.Common;
using DevExpress.Web.Mvc;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 文件阅办类别定义
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class TaskClassController : AdminBaseController
    {
        /// <summary>
        /// 文件阅办类别定义
        /// </summary>
        /// <param name="focusNodeName">选中节点</param>
        /// <returns>主页面</returns>
        [AuthorityActionFilter("m_TaskClass", Order = 1)]
        [LogPageActionFilter("文件阅办类别定义", Order = 2)]
        public ActionResult Index(string focusNodeName = "")
        {
            ViewData["focusNodeName"] = focusNodeName;
            BLL.CdTaskClass TaskBLL = new BLL.CdTaskClass();
            List<Model.CdTaskClass> prvGroupList = TaskBLL.GetModelList("");
            ViewData["isHaveTreeData"] = prvGroupList.Count == 0 ? false : true;
            return View();
        }

        public ActionResult TaskClassEdit(string taskClassCd)
        {
            return EditGroup(taskClassCd, "", "EDIT", null);
        }

        public ActionResult TaskClassTree(string focusNodeName = "")
        {
            BLL.CdTaskClass CdTaskClassBLL = new BLL.CdTaskClass();
            List<Model.CdTaskClass> CdTaskClassList = CdTaskClassBLL.GetModelList("");
            ElingOA6_Admin_DSJCJ.Models.TreeModelList treeModelList = new TreeModelList();
            if (CdTaskClassList.Count > 0)
            {
                foreach (Model.CdTaskClass Model in CdTaskClassList)
                {
                    TreeItemModel treeParentItemModel = new TreeItemModel(null)
                    {
                        Id = DevExpressHelper.GetUrl(new { Controller = "TaskClass", Action = "TaskClassEdit" }) + "|" + Model.FTaskClassCd,
                        Name = Model.FTaskClassName,
                        ImageUrl = Url.Content("~/Content/ControlImages/user_group.png")
                    };
                    treeModelList.Add(treeParentItemModel);
                }
            }
            ViewData["focusNodeName"] = focusNodeName;
            return View(treeModelList);
        }

        public ActionResult Save(Model.CdTaskClass CdTaskClassModel)
        {
            bool isSuccess = true;
            string errorMessage = "";
            ActionResult actionResult = null;
            //model赋值
            //Model.PrvGroup prvGroupModel = new Model.PrvGroup()
            //{
            //    FGroupName = Fetch.Post("txtFGroupName"),
            //    FNote = Fetch.Post("txtFNote")
            //};
            string taskClassCd = "0";
            //数据逻辑验证
            if (CdTaskClassModel.FTaskClassName == "")
            {
                errorMessage = "名称不能为空";
                isSuccess = false;
            }
            //CdTaskClassModel.FEntityNo = GlobalConstant.ENTITYNO;
            List<Model.TaskAuthority> TaskAuthorityList = new List<Model.TaskAuthority>();

            BLL.CdTaskClass CdTaskClassBLL = new BLL.CdTaskClass();
            string funcs = Fetch.Post("hddjUserSelect"); 
            List<string> userFuncList = new List<string>();
            if (funcs != "")
            {
                userFuncList = funcs.Split(new char[1] { ',' }).ToList();
            }
            foreach (string str in userFuncList)
            {
                Model.TaskAuthority TaskAuthorityModel = new Model.TaskAuthority();
                TaskAuthorityModel.FEntityNo = GlobalConstant.ENTITYNO;
                TaskAuthorityModel.FLogID = str;
                TaskAuthorityModel.FTaskClassCd = CdTaskClassModel.FTaskClassCd;
                TaskAuthorityModel.FAuthorityType = "ADD";
                TaskAuthorityList.Add(TaskAuthorityModel);
            }

            string users = Fetch.Post("hdspUserSelect");
            List<string> orgUserList = new List<string>();
            if (users != "")
            {
                orgUserList = users.Split(new char[1] { ',' }).ToList();
            }

            foreach (string str in orgUserList)
            {
                Model.TaskAuthority TaskAuthorityModel = new Model.TaskAuthority();
                TaskAuthorityModel.FEntityNo = GlobalConstant.ENTITYNO;
                TaskAuthorityModel.FLogID = str;
                TaskAuthorityModel.FTaskClassCd = CdTaskClassModel.FTaskClassCd;
                TaskAuthorityModel.FAuthorityType = "AUTH";
                TaskAuthorityList.Add(TaskAuthorityModel);
            }

            switch (Fetch.Post("hdMethod"))
            {
                case "ADD_SAME":
                    {
                        if (CdTaskClassBLL.Exists(CdTaskClassModel.FTaskClassCd))
                        {
                            errorMessage = "用户组编号已存在";
                            isSuccess = false;
                        }
                        else
                        {
                            try
                            {
                                CdTaskClassBLL.Add(CdTaskClassModel, TaskAuthorityList);
                                taskClassCd = CdTaskClassModel.FTaskClassCd;
                                //增加成功
                                ViewData["focusKey"] = DevExpressHelper.GetUrl(new { Controller = "TaskClass", Action = "TaskClassEdit" }) + "|" + taskClassCd;//焦点定位的节点ID
                                ViewData["methodFromType"] = Fetch.Post("hdMethod");
                                ViewData["nodeText"] = CdTaskClassModel.FTaskClassName;
                                actionResult = View("SuccessMessageByTree");
                            }
                            catch
                            {
                                errorMessage = "操作异常:";
                            }


                        }
                        if (actionResult == null)
                        {
                            actionResult = EditGroup("0", errorMessage, Fetch.Post("hdMethod"), new Model.CdTaskClass());
                        }
                        break;
                    }
                case "EDIT":
                    {
                        if (isSuccess)
                        {
                            try
                            {
                                CdTaskClassBLL.Update(CdTaskClassModel, TaskAuthorityList);
                                //修改成功
                                ViewData["RedirectURL"] = Url.Action("TaskClassEdit", "TaskClass", new { taskClassCd = CdTaskClassModel.FTaskClassCd });
                                ViewData["methodFromType"] = Fetch.Post("hdMethod");
                                ViewData["nodeText"] = CdTaskClassModel.FTaskClassName;
                                actionResult = View("SuccessMessageByTree");
                            }
                            catch
                            {
                                errorMessage = "操作异常:";
                            }
                        }
                        if (actionResult == null)
                        {
                            actionResult = EditGroup(CdTaskClassModel.FTaskClassCd, errorMessage, Fetch.Post("hdMethod"), CdTaskClassModel);
                        }
                        break;
                    }
            }

            return actionResult;
        }

        public string DeleteTaskClass(string FTaskClassCdEn)
        {
            try
            {
                string FTaskClassCd = GenerateUtil.Decrypt(FTaskClassCdEn, "FTaskClassCd");
                BLL.CdTaskClass CdTaskClassBLL = new BLL.CdTaskClass();
                CdTaskClassBLL.Delete(FTaskClassCd);
                return "ok";
            }
            catch
            {
                return "error";
            }
        }
              
        /// <summary>
        /// 增加文件阅办类别
        /// </summary>
        /// <returns>返回增加视图</returns>
        public ActionResult AddTaskClass()
        {
            //return AddGroup("", null);
            return EditGroup("0", "", "ADD_SAME", new Model.CdTaskClass());
        }
        /// <summary>
        /// 修改文件阅办类别
        /// </summary>
        /// <param name="groupID">类别编号</param>
        /// <param name="jsAlert">显示文字</param>
        /// <param name="methodType">页面类型  ADD 新增; EDIT 修改</param>
        /// <param name="prvGroupModel">类别数据</param>
        /// <returns>返回增加或修改视图</returns>
        private ActionResult EditGroup(string taskClassCd, string jsAlert, string methodType, Model.CdTaskClass CdTaskClassModel)
        {
            BLL.CdTaskClass CdTaskClassBLL = new BLL.CdTaskClass();
            BLL.PrvUser prvUserBll = new BLL.PrvUser();
            BLL.Index prvfunBll = new BLL.Index();
            //增加和修改同一个页面
            ViewData["hdMethod"] = methodType;//类型  ADD_SAME 新增; EDIT 修改
            if (CdTaskClassModel == null)
            {
                CdTaskClassModel = CdTaskClassBLL.GetModel(taskClassCd);
            }
            if (CdTaskClassModel != null)
            {
                ElingOA6_Admin_DSJCJ.Models.TreeModelList treeUserOrgModelList = new TreeModelList();
                CdTaskClassModel.FTaskClassCdEn = GenerateUtil.Encrypt(CdTaskClassModel.FTaskClassCd == null ? "0" : CdTaskClassModel.FTaskClassCd.ToString(), "FTaskClassCd");
                if (CdTaskClassModel != null)
                {
                    BLL.HRDept deptBll = new BLL.HRDept();
                    List<Model.HRDept> deptList = deptBll.GetListBYFIsAgency(GlobalConstant.ENTITYNO, "");
                    List<Model.PrvUser> prvUserList = prvUserBll.GetListDept(GlobalConstant.ENTITYNO);//获取t_PrvUser所有用户
                    foreach (Model.HRDept deptObj in deptList.Where(c => c.FParentDeptNo == null))
                    {
                        TreeItemModel treeParentItemModel = new TreeItemModel(null)
                        {
                            Id = "dept_" + deptObj.FDeptNo,
                            Name = deptObj.FDeptName,
                            ImageUrl = Url.Content("~/Content/ControlImages/dept_Groups.png")
                        };

                        SetUser(deptObj.FDeptNo, prvUserList, treeParentItemModel);

                        SetChildDept(deptObj.FDeptNo, deptList, prvUserList, treeParentItemModel);


                        treeUserOrgModelList.Add(treeParentItemModel);

                    }
                }
                ViewData["treeModelUserList"] = treeUserOrgModelList;

                //隐藏字段
                BLL.TaskAuthority TaskAuthorityBLL=new BLL.TaskAuthority();
                List<Model.TaskAuthority> loginIDList = TaskAuthorityBLL.GetModelList(" FTaskClassCd='" + taskClassCd + "'");//当前用户组的所有成员
                ViewData["jsAlert"] = jsAlert;
                ViewData["taskClassCd"] = taskClassCd;
                string sploginIDs = "";
                string djloginIDs = "";
                if (loginIDList.Count > 0)
                {
                    foreach (Model.TaskAuthority s in loginIDList)
                    {
                        if (s.FAuthorityType == "ADD")
                        {
                            djloginIDs += s.FLogID + ",";
                        }
                        else
                        {
                            sploginIDs += s.FLogID + ",";
                        }
                    }
                    if (sploginIDs != "")
                    {
                        sploginIDs = sploginIDs.Substring(0, sploginIDs.LastIndexOf(","));
                    }
                    if (djloginIDs != "")
                    {
                        djloginIDs = djloginIDs.Substring(0, djloginIDs.LastIndexOf(","));
                    }
                }

                ViewData["djloginIDs"] = djloginIDs;
                ViewData["sploginIDs"] = sploginIDs;
                return View("TaskClassEdit", CdTaskClassModel);
            }
            return null;
        }

        /// <summary>
        /// 绑定下级部门
        /// </summary>
        /// <param name="preDeptNo">父级部门编号</param>
        /// <param name="deptList">部门数据集合</param>
        /// <param name="userList">用户数据集合</param>
        /// <param name="treeParentItemModel">父级树节点</param>
        private void SetChildDept(string preDeptNo, List<Model.HRDept> deptList, List<Model.PrvUser> userList, TreeItemModel treeParentItemModel)
        {
            foreach (Model.HRDept deptObj in deptList.Where(c => c.FParentDeptNo == preDeptNo))
            {
                TreeItemModel treeChildItemModel = new TreeItemModel(treeParentItemModel)
                {
                    Id = "dept|" + deptObj.FDeptNo,
                    Name = deptObj.FDeptName,
                    ImageUrl = Url.Content("~/Content/ControlImages/dept_Groups.png")
                };

                SetUser(deptObj.FDeptNo, userList, treeChildItemModel);

                SetChildDept(deptObj.FDeptNo, deptList, userList, treeChildItemModel);
            }
        }


        /// <summary>
        /// 绑定部门人员
        /// </summary>
        /// <param name="deptNo">部门编号</param>
        /// <param name="userList">用户数据列表</param>
        /// <param name="treeParentItemModel">父级树节点</param>
        private void SetUser(string deptNo, List<Model.PrvUser> userList, TreeItemModel treeParentItemModel)
        {
            foreach (Model.PrvUser prvModel in userList.Where(c => c.FDeptNo == deptNo))
            {
                TreeItemModel treeChildItemModel = new TreeItemModel(treeParentItemModel)
                {
                    Id = "user" + "|" + prvModel.FLogID,
                    Name = prvModel.FUserName,
                    ImageUrl = Url.Content("~/Content/ControlImages/user_male.png")
                };
            }
        }

    }
}
